package com.eco.fanliapp.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.eco.fanliapp.common.constant.ApiEnum;

/**
 * @author zhuchao
 * @create 2020-03-10 9:42
 */
public class AliYunMesUtils {

	static final String accessKeyId = ApiEnum.ACCESSKEYID;  // TODO 修改成自己的
	static final String accessKeySecret = ApiEnum.ACCESSKEYSECRET;   // TODO 修改成自己的

	/**
	 * SendSms接口是短信发送接口，支持在一次请求中向多个不同的手机号码发送同样内容的短信。
	 * @param phoneNumber
	 * @param signName
	 * @param templateCode
	 * @param templateParam
	 * @return
	 */
		public static JSONObject SendSms(String phoneNumber,String signName,String templateCode,String templateParam) {
			//DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fggg71m9wvDhN3AiinZ", "5qXJd58cW96NwebiXxfEZmSsDwu4kx");
			DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			IAcsClient client = new DefaultAcsClient(profile);

			CommonRequest request = new CommonRequest();
			request.setSysMethod(MethodType.POST);
			request.setSysDomain("dysmsapi.aliyuncs.com");
			request.setSysVersion("2017-05-25");
			request.setSysAction("SendSms");
			request.putQueryParameter("RegionId", "cn-hangzhou");

			//此处放接收验证码的手机号
			request.putQueryParameter("PhoneNumbers", phoneNumber);
			//此处放签名名称（必须审核通过）
			request.putQueryParameter("SignName", signName);
			//此处放短信模板（必须审核通过）
			request.putQueryParameter("TemplateCode", templateCode);
			//此处放验证码的内容（JSON格式\"表示转义。JSON格式：{"code":"666666"}）
			request.putQueryParameter("TemplateParam", templateParam);

			try {
				CommonResponse response = client.getCommonResponse(request);
				JSONObject json = JSONObject.parseObject(new String(response.getData()));
				return json;
				//System.out.println(response.getData());
			} catch (ServerException e) {
				e.printStackTrace();
			} catch (ClientException e) {
				e.printStackTrace();
			}
			return null;
		}

		public static void main(String[] args) {
			String accessKeyId = "LTAI4Fggg71m9wvDhN3AiinZ";  // TODO 修改成自己的
			String accessKeySecret = "5qXJd58cW96NwebiXxfEZmSsDwu4kx";   // TODO 修改成自己的
			JSONObject jsonObject = SendSms("15117916955", "省鸭", "SMS_185255059", "测试的");
			System.out.println(jsonObject);
		}
}
